package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 董代恒
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理接口")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result saveDish(@RequestBody DishDTO dishDTO) {
        log.debug("新增菜品:disDTO:{}", dishDTO);
        dishService.saveDishWithFlavors(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> selectPageDish(DishPageQueryDTO dishPageQueryDTO) {
        log.debug("菜品分页查询:dishPageQueryDTO:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.selectPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @param ids
     * @return
     * @RequestParam：只有加了这个主键SpringMVC框架才会将?ids=1,2,3这些形式的路径参数封装到List集合中
     */
    //TODO:为什么加@RequestParam注解后SpringMVC框架会将?ids=1,2,3这些形式的路径参数封装到List集合中
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result deleteBathDish(@RequestParam List<Long> ids) {
        log.debug("删除的ids:ids:{}", ids);
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        log.debug("分类id:categoryId:{}", categoryId);
        List<Dish> dishList = dishService.selectDishByCategoryId(categoryId);
        return Result.success(dishList);
    }


}
